$(function () {
  loadUninstallIntellGridTable();
});

function getTotalCount(obj){
	var totalCount = obj.getGridParam('records');
	if(totalCount == null || totalCount == '')
		return 0;
	return totalCount;
}

function loadUninstallIntellGridTable() {
	$("#uninstallIntellGridTable").jqGrid({url:"using_batch.action", 
										datatype:"json", 
										height:200, 
										width:820, 
										colNames:["给哪些宝贝安装", "安装位置","静态推广宝贝", "动态推广宝贝","更换频率","配置编码", "给哪些宝贝安装编码"], 
										colModel:[  {name:"INSTALL_SETTING_DESC", index:"INSTALL_SETTING_DESC", align:"center", width:"120"},
            										{name:"INSTALL_PLACE_DESC", index:"INSTALL_PLACE_DESC"},			
                                                    {name:"STATIC_NUM_DESC", index:"STATIC_NUM_DESC", align:"center", width:"110"},
													{name:"INTELL_SETTING_DESC", index:"INTELL_SETTING_DESC", align:"center", width:"200"},
													{name:"FREQUENCY_DESC", index:"FREQUENCY_DESC",align:"center"},
                                                    {name:"PRO_ID", index:"PRO_ID", hidden:true},
													{name:"INSTALL_SETTING", index:"INSTALL_SETTING", hidden:true}] ,    
									  	viewrecords:true, 
									  	rowNum:5, 
									  	rowList:[5, 8, 10], 
                                        gridComplete:function () {
                                          if(is_display("uninstallIntellGridTable")){
                                            setImageCellHeight("uninstallIntellGridTable");
                                          }//假如没有数据就不要展示了
                                        },
									  	loadComplete:function () {
										}, 
									    onSelectRow: function (rowid, status) {
									      if (rowid === null) {
									        return;
									      }
									    },
									    postData:{
											'pager.totalCount':function(){return getTotalCount($("#uninstallIntellGridTable"));}
										},
										jsonReader:{root:"dataset", 
													page:"pager.curPageNo", 
													total:"pager.totalPages", 
													records:"pager.totalCount", 
													repeatitems:false
										}, 
										prmNames:{rows:"pager.pageSize", 
												  page:"pager.curPageNo", 
												  sort:"pager.orderBy", 
												  order:"pager.order"
										}, 
										pager:"#uninstallIntellGridPager", 
										caption:"正在使用的推广配置",
										subGrid: true,
										subGridRowExpanded: function(subgrid_id, row_id) {
											var ret = $("#uninstallIntellGridTable").jqGrid("getRowData", row_id);
											if(ret.INSTALL_SETTING == "someCategory"){
												var subgrid_table_id = subgrid_id+"_t";
												var pager_id = "p_"+subgrid_table_id;
												$("#"+subgrid_id).html("<table id='"+subgrid_table_id+"'></table><div id='"+pager_id+"'></div>");
												$("#"+subgrid_table_id).jqGrid({
														url:"querySomeCategory.action?PRO_ID="+ret.PRO_ID,
														datatype: "json",
														width:200,
														colNames: ["分类名称","分类编码"],
														colModel: [ {name:"CNAME", index:"CNAME", width:"90"},
																	{name:"CID", index:"CID", align:"center",hidden:true}],
													   	rowNum:5,
													   	pager: pager_id,
													    height: '100%',
													    jsonReader:{root:"dataset", 
															page:"pager.curPageNo", 
															total:"pager.totalPages", 
															records:"pager.totalCount", 
															repeatitems:false
														}, 
														prmNames:{rows:"pager.pageSize", 
																  page:"pager.curPageNo", 
																  sort:"pager.orderBy", 
																  order:"pager.order"
														}, 
														postData:{
															'pager.totalCount':function(){return getTotalCount($("#"+subgrid_table_id));}
														}
												});
											}
											else if(ret.INSTALL_SETTING == "custom"){
												var subgrid_table_id = subgrid_id+"_t";
													var pager_id = "p_"+subgrid_table_id;
													$("#"+subgrid_id).html("<table id='"+subgrid_table_id+"'></table><div id='"+pager_id+"'></div>");
													$("#"+subgrid_table_id).jqGrid({
															url:"queryCustom.action?PRO_ID="+ret.PRO_ID,
															datatype: "json",
															width:200,
															colNames: ["图片","宝贝"],
															colModel: [ {name:"IMG", index:"IMG", formatter:formateadorLink1, sortable:false,align:"center", width:"60px"}, 
																		{name:"TITLE", index:"TITLE", formatter:formateadorLink2, align:"center", width:"120"}],
														   	rowNum:5,
														   	pager: pager_id,
														    height: '100%',
														    jsonReader:{root:"dataset", 
																page:"pager.curPageNo", 
																total:"pager.totalPages", 
																records:"pager.totalCount", 
																repeatitems:false
															}, 
															prmNames:{rows:"pager.pageSize", 
																	  page:"pager.curPageNo", 
																	  sort:"pager.orderBy", 
																	  order:"pager.order"
															}, 
															postData:{
																'pager.totalCount':function(){return getTotalCount($("#"+subgrid_table_id));}
															}
													});
											}
											
											//$("#"+subgrid_table_id).jqGrid('navGrid',"#"+pager_id,{edit:false,add:false,del:false})
										},
										subGridRowColapsed: function(subgrid_id, row_id) {
											var subgrid_table_id = subgrid_id+"_t";
											$("#"+subgrid_table_id).remove();
										}
										
										
	});
	//$("#uninstallIntellGridTable").jqGrid('navGrid','#uninstallIntellGridPager',{add:false,edit:false,del:false});
}

function formateadorLink1(cellvalue, options, rowObject) {
	return "<img class='item_img' src='"+cellvalue+"' />";
}

function formateadorLink2(cellvalue, options, rowObject) {
	var title = "<span style='text-decoration:underline;font-size:13px;color:blue;'>" + cellvalue + "</span>";
	return "<a href=http://item.taobao.com/item.htm?id=" + rowObject.IID + " target='_blank'>" + title + "</a>";
}

function reExecute(obj,batchId){
	obj.disabled = true;
	$.post("reExecute.action", "BATCH_ID="+batchId);
}

function formateadorLink1(cellvalue, options, rowObject) {
	return "<img class='item_img' src='"+cellvalue+"_60x60.jpg' />";
}

function formateadorLink2(cellvalue, options, rowObject) {
	var title = "<span style='text-decoration:underline;font-size:13px;color:blue;'>" + cellvalue + "</span>";
	return "<a href=http://item.taobao.com/item.htm?id=" + rowObject.IID + " target='_blank'>" + title + "</a>";
}

function setImageCellHeight(gridName) {
	var ids = $("#" + gridName).getDataIDs();
	for (var i = 0; i < ids.length; i++) {
		var cl = ids[i];
		$("#" + gridName).setRowData(ids[i], false, {height:40});
	}
}

/**
*没有配置就展示按钮
*/
function is_display(gridName){
 var rowNum= $("#" + gridName).jqGrid('getGridParam','records');
 if(rowNum==0){
   $("#start_ipro_div").html('');
   $("#start_now_pic").show();
   return false;
 }
 $("#start_ipro_div").show();
 return true;
}
